/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/core/pgl.h>
#include <pgl/core/PGLLineVer.h>
#include <pgl/core/PGLRegion.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif



PGL_IMPLEMENT_DYNCREATE(CPGLLineVer, CPGLLine);

//! Save settings
void CPGLLineVer::SerializeXML(CPGLArchive& ar)
{
	static const TCHAR szTag[] = _T("CPGLLineVer");

	markup::CMarkupArchive& xml = *((markup::CMarkupArchive*)ar.GetMarkup());
	if(ar.IsStoring())
	{
		xml.AddChildElem(szTag);
		xml.AddChildAttrib(_T("Name"),GetName());
		xml.IntoElem();

			/////////////////////////////////////////////////////////
			// Adding base class
			CPGLLine::SerializeXML(ar);

			xml.AddChildElemEx(_T("X"),m_dX);
			xml.AddChildElem(_T("Caption"));
			xml.IntoElem();
				m_tCaption.SerializeXML(ar);
			xml.OutOfElem();

		xml.OutOfElem();
	}
	else
	{
		if  (!xml.FindChildElem(szTag))
				return;

		xml.IntoElem();

			/////////////////////////////////////////////////////////
			// loading base class
			CPGLLine::SerializeXML(ar);

			if (xml.FindChildElem(_T("X")))
				m_dX=xml.GetChildDataDouble();

			xml.ResetChildPos();
			if(xml.FindChildElem(_T("Caption")))
			{
				xml.IntoElem();
					m_tCaption.SerializeXML(ar);
				xml.OutOfElem();
			}

		xml.OutOfElem();

		NeedUpdateExtent();
	}

}

#ifdef _DEBUG
void CPGLLineVer::Dump( CDumpContext& dc ) const
{
    // call base class function first
    CPGLLine::Dump( dc );
}
void CPGLLineVer::AssertValid() const
{
    // call inherited AssertValid first
    CPGLLine::AssertValid();

} 
#endif


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPGLLineVer::CPGLLineVer()
: CPGLLine()
{
	m_dX = 0;
	m_tCaption.SetAngle(-90);
	m_tCaption.SetAlign(gfxinterface::TextAlignmentLeft);
}

CPGLLineVer& CPGLLineVer::operator =(const CPGLLineVer& lv)
{
	if (this != &lv)
	{
		this->CPGLLine::operator =(lv);

		m_dX = lv.m_dX;
		m_tCaption = lv.m_tCaption;
	}
	return *this;
}

CPGLLineVer::~CPGLLineVer()
{
}

void CPGLLineVer::GetUnionExtent(gfxinterface::CGfxInterface& gfx, CPGLRectD& rExtent)
{
	rExtent.MakeUnionX(m_dX);

	rExtent.xValid=true;
	rExtent.yValid=false;
}

void CPGLLineVer::PlotGfx(gfxinterface::CGfxInterface& gfx)
{
	using namespace gfxinterface;
	if (!IsVisible())
		return;

	// calling base class plot
	CPGLObject::PlotGfx(gfx);
	
	GFX_ADDCOMMENT(gfx,"--- CPGLLineVer ---");	

	// Sets dash...
	SetDashGfx(gfx);	
	// set line width
	gfx.SetLineWidth(GetLineWidth());	
	gfx.DrawLine(m_dX, gfx.GetWorldBottom(), m_dX, gfx.GetWorldTop());

	// setting caption position
	CPGLCoord2D& pos=m_tCaption.GetPosition();
	pos.SetPosition( 0, 1, CPGLCoord2D::Coord2DNormClip);
	pos.SetBBTrans(SPointD( -GetLineWidth(), -15));
	pos.SetWorldTrans(SPointD(m_dX, 0));
//	float pos[2];
//	pos[0]=(float)(m_dX-gfx.BBToWorldAbsx( GetLineWidth() ));
//	pos[1]=(float)(gfx.GetWorldClipTop()-gfx.BBToWorldAbsy(15));
//	m_tCaption.SetPosition(pos);
	m_tCaption.PlotGfx(gfx);
}


void CPGLLineVer::PlotLegendGfx(gfxinterface::CGfxInterface& gfx, const CPGLRectD& rBox)
{
	if (!IsVisible())
		return;

	// calling base class plot
	CPGLObject::PlotGfx(gfx);
	// Sets dash...
	SetDashGfx(gfx);	
	// set line width
	gfx.SetLineWidth(GetLineWidth());	

	double x = rBox.left + rBox.GetWidth()/2.0;
	gfx.DrawLine(x, rBox.bottom, x, rBox.top);
}
